package com.zmn.plat.admin.controller.brand.ratio;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.BaseBrandPriceTypeEnum;
import com.zmn.plat.common.dictionary.BaseBrandRatioTypeEnum;
import com.zmn.plat.model.entity.brand.BaseBrand;
import com.zmn.plat.model.entity.brand.BaseBrandQuery;
import com.zmn.plat.model.entity.brand.BrandRatio;
import com.zmn.plat.model.entity.brand.BrandRatioQuery;
import com.zmn.plat.model.vo.brand.BrandRatioVo;

/**
 * 类描述:城市价格系数管理查询控制
 *
 * @author linfeng
 * @since 2019/08/21/11:02
 */
@Controller
@RequestMapping("/brand/ratio/")
public class BrandRatioListController extends BrandRatioBaseController {
    private Logger logger = LoggerFactory.getLogger(BrandRatioListController.class);

    /**
     * 描述: 品牌系数列表
     *
     * @param query
     * @return ModelAndView
     */
    @RequestMapping(value = "list")
    public ModelAndView list(@ModelAttribute BrandRatioQuery query) {
        if (query.getPriceType() == null) {
            query.setPriceType(BaseBrandPriceTypeEnum.PRICE.getCode());
        }
        query.setRatioType(BaseBrandRatioTypeEnum.BRANDRATIO.getCode());
        ModelAndView mav = new ModelAndView("brand/ratio/list");
        try {
            List<BrandRatioVo> items = brandRatioService.listPageByQuery(query);
            mav.addObject("vo", query);
            mav.addObject("items", items);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return mav;
    }

    /**
     * 品牌分类保内系数(一二级保内分类系数对应表)
     *
     * @param brandId
     * @param servCategId
     * @param brandName
     * @return
     */
    @RequestMapping(value = "categoryInRatio")
    public ModelAndView categoryInRatio(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "brandName", defaultValue = "") String brandName) {
        return categoryRatio(brandId, priceType, servCategId, ProductConsts.BIZ_TYPE_F, brandName);
    }

    /**
     * 品牌分类保外系数(一二级保外分类系数对应表)
     *
     * @param brandId
     * @param servCategId
     * @param brandName
     * @return
     */
    @RequestMapping(value = "categoryOutRatio")
    public ModelAndView categoryOutRatio(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "brandName", defaultValue = "") String brandName) {
        return categoryRatio(brandId, priceType, servCategId, ProductConsts.BIZ_TYPE_C, brandName);
    }

    /**
     * 品牌分类工程系数(一二级保外分类系数对应表)
     *
     * @param brandId
     * @param servCategId
     * @param brandName
     * @return
     */
    @RequestMapping(value = "categoryProjectRatio")
    public ModelAndView categoryProjectRatio(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "brandName", defaultValue = "") String brandName) {
        return categoryRatio(brandId, priceType, servCategId, ProductConsts.BIZ_TYPE_B, brandName);
    }

    /**
     * 品牌分类计价器3.0系数(一二级保外分类系数对应表)
     *
     * @param brandId
     * @param servCategId
     * @param brandName
     * @return
     */
    @RequestMapping(value = "categoryMeterRatio")
    public ModelAndView categoryMeterRatio(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
                                           @RequestParam(value = "priceType", defaultValue = "1") int priceType,
                                           @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
                                           @RequestParam(value = "brandName", defaultValue = "") String brandName) {
        return categoryRatio(brandId, priceType, servCategId, 4, brandName);
    }

    /**
     * 保内系数-设置分类系数
     *
     * @param brandId
     * @param servCategId
     * @param categOneId
     * @param categOneName
     * @param brandName
     * @param servCategName
     * @return
     */
    @RequestMapping(value = "categDetail")
    public ModelAndView categDetail(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "bizType", defaultValue = "1") int bizType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "categOneId", defaultValue = "0") int categOneId,
        @RequestParam(value = "categOneName", defaultValue = "0") String categOneName,
        @RequestParam(value = "brandName", defaultValue = "") String brandName,
        @RequestParam(value = "servCategName", defaultValue = "") String servCategName) {

        return detail(brandId, priceType, servCategId, categOneId, categOneName, bizType, brandName, servCategName);
    }

    /**
     * 品牌默认系数
     *
     * @param brandId
     * @return
     */
    @RequestMapping("brandRatio")
    public ModelAndView brandRatio(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "bizType", defaultValue = "0") int bizType) {
        BrandRatioQuery brandRatioQuery = new BrandRatioQuery();
        brandRatioQuery.setBrandId(brandId);
        brandRatioQuery.setRatioType(BaseBrandRatioTypeEnum.BRANDRATIO.getCode());
        brandRatioQuery.setBizType(ProductConsts.BIZ_TYPE_C);
        brandRatioQuery.setPriceType(priceType);
        List<BrandRatio> brandRatios = brandRatioService.listByQuery(brandRatioQuery);
        BrandRatio brandRatioC = new BrandRatio();
        if (brandRatios != null && brandRatios.size() > 0) {
            brandRatioC = brandRatios.get(0);
        } else {
            brandRatioC = new BrandRatio();
        }

        brandRatioQuery.setBizType(ProductConsts.BIZ_TYPE_F);
        brandRatios = brandRatioService.listByQuery(brandRatioQuery);
        BrandRatio brandRatioF = new BrandRatio();
        if (brandRatios != null && brandRatios.size() > 0) {
            brandRatioF = brandRatios.get(0);
        } else {
            brandRatioF = new BrandRatio();
        }

        brandRatioQuery.setBizType(ProductConsts.BIZ_TYPE_B);
        brandRatios = brandRatioService.listByQuery(brandRatioQuery);
        BrandRatio brandRatioB = new BrandRatio();
        if (brandRatios != null && brandRatios.size() > 0) {
            brandRatioB = brandRatios.get(0);
        } else {
            brandRatioB = new BrandRatio();
        }

        brandRatioQuery.setBizType(null);
        brandRatioQuery.setRatioType(BaseBrandRatioTypeEnum.BRANDMETER3RATIO.getCode());
        brandRatios = brandRatioService.listByQuery(brandRatioQuery);
        BrandRatio brandRatioM = new BrandRatio();
        if (brandRatios != null && brandRatios.size() > 0) {
            brandRatioM = brandRatios.get(0);
        }

        if (NumberUtil.isNotNullOrZero(brandId) && NumberUtil.isNotNullOrZero(priceType)) {
            brandRatioC.setPriceType(priceType);
            brandRatioF.setPriceType(priceType);
            brandRatioB.setPriceType(priceType);
            brandRatioM.setPriceType(priceType);
        }

        BaseBrandQuery baseBrandQuery = new BaseBrandQuery();
        baseBrandQuery.setStatus(GlobalConsts.YES);
        List<BaseBrand> baseBrandList = baseBrandService.listByQuery(baseBrandQuery);
        ModelAndView mav = new ModelAndView("brand/ratio/brandratio");

        mav.addObject("brandRatioC", brandRatioC);
        mav.addObject("brandRatioF", brandRatioF);
        mav.addObject("brandRatioB", brandRatioB);
        mav.addObject("brandRatioM", brandRatioM);
        mav.addObject("brandId", brandId);
        mav.addObject("bizType", bizType);
        mav.addObject("priceType", priceType);
        mav.addObject("baseBrandList", baseBrandList);
        return mav;
    }
}
